<?php
namespace app\index\controller;

use app\index\model\Article;
use think\Controller;
use think\Db;
use think\Log;
use think\Request;
use think\Url;
use think\Validate;

class Demo extends Controller {

    public function index($name = "小明") {
        $params = $this->request->param();

        echo '姓名:' . $name;

        p($params);
    }

    public function hello($name) {
        $params = $this->request->param();

        echo '姓名:' . $name;

        p($params);
    }

    public function today($year, $month) {
        echo $year . '年' . $month . '月';
    }

    public function url() {
        echo Url::build('url2', 'a=1&b=2');
        echo '<hr>';
        echo url('url2', 'a=1&b=2');
        echo '<hr>';
        echo url('url2', ['a' => 1, 'b' => 2]);
        echo '<hr>';
        echo url('admin/index/url2', 'a=1&b=2');
        echo '<hr>';
        echo url('admin/hello/index');
        echo '<hr>';
        $request = Request::instance();
        // 获取当前域名
        echo 'domain: ' . $request->domain() . '<br/>';
        // 获取当前入口文件
        echo 'file: ' . $request->baseFile() . '<br/>';
        // 获取当前URL地址 不含域名
        echo 'url: ' . $request->url() . '<br/>';
        // 获取包含域名的完整URL地址
        echo 'url with domain: ' . $request->url(true) . '<br/>';
        // 获取当前URL地址 不含QUERY_STRING
        echo 'url without query: ' . $request->baseUrl() . '<br/>';
        // 获取URL访问的ROOT地址
        echo 'root:' . $request->root() . '<br/>';
        // 获取URL访问的ROOT地址
        echo 'root with domain: ' . $request->root(true) . '<br/>';
        // 获取URL地址中的PATH_INFO信息
        echo 'pathinfo: ' . $request->pathinfo() . '<br/>';
        // 获取URL地址中的PATH_INFO信息 不含后缀
        echo 'pathinfo: ' . $request->path() . '<br/>';
        // 获取URL地址中的后缀信息
        echo 'ext: ' . $request->ext() . '<br/>';
    }

    public function req() {

        dump(Request::instance()->get());
        echo Request::instance()->query();
        echo '<hr>';
        $request = Request::instance();
        echo "当前模块名称是" . $request->module();
        echo '<hr>';
        echo "当前控制器名称是" . $request->controller();
        echo '<hr>';
        echo "当前操作名称是" . $request->action();
    }

    public function mydb() {

        /*
        # 添加数据
        $data = [
            'title' => '测试1',
            'info' => '测试内容1',
            'intime' => time(),
            'indate' => date('Y-m-d'),
            'ip' => $this->request->ip()
        ];

        $artid = Db::name('article')->insertGetId($data);
        return $artid;
        */

        /*
        # 查询
        $list = Article::where(['state' => 1])->select();
        foreach ($list as $obj) {
            echo $obj->title . '<hr>';
        }

        $list = Article::where('id', 1)->find();
        echo $list->title . '<hr>';

        $list = Db::select(function ($query) {
           $query->name('article')->where(['state'=>1]);
        });
        foreach ($list as $obj) {
            echo $obj['title'] . '<hr>';
        }

        $title = Article::whereor(['id'=>1,'state'=>1])->value('title');
        echo Article::getLastSql().' --- ';
        echo $title. '<hr>';

        $list = Article::where(['state'=>1])->column('title','id');
        p($list);

        $list = Article::where(['state'=>['>=',1]])->column('title','id');
        p($list);
        */


        # 修改
        /*
        $ret = Article::where('id',1)->update(['title'=>'修改一下就知道'.time()]);
        dump($ret);
        echo '<hr>';

        $ret = Article::where(['id'=>1])->update([
            'intime' => ['exp','unix_timestamp(now())'],
            'nums' => ['exp','nums+1']
        ]);
        dump($ret);
        echo '<hr>';


        $ret = Article::where(['id'=>1])->setField(['title'=>'修改你了']);
        dump($ret);
        echo '<hr>';
        */

        #p(Article::getTableInfo());

        /*
        Article::where(['id'=>['in',[1,2]]])->select();
        Article::where('id','exp','IN(1,2) and state=1')->select();
        */

        #Article::limit(1,1)->select();

        /*
        $total = Article::count();
        echo $total.'<hr>';

        $max = Article::max('nums');
        echo $max.'<hr>';
        */

        /*
        $list = Article::whereTime('intime','>=','2016-11-14')->select();
        dump($list);
        */

        /*
        $list = Article::whereTime('intime','between',['2016-11-14','2016-11-15'])->select();
        dump($list);
        */


        #Article::where('title|info','like','%测试%')->select();
        #Article::where('title','like','%测试%')->where('intime&nums','>',0)->select();
        #Article::where('id',['>',0],['<>','2'],'or')->select();

        /*
        Article::where(function($query){
            $query->where(['title'=>['like','%php%']])->whereor(['info'=>['like','%java%']]);
        })->whereOr(['id'=>['>',2]])->select();
        */

        /*
        # sql监听
        Db::listen(function($sql, $time, $explain){
            // 记录SQL
            echo $sql. ' ['.$time.'s]<hr>';
            // 查看性能分析结果
            #dump($explain);
        });


        Article::where("id>:id and title like :title",['id'=>0,'title'=>'%php%'])->select();
        */

        /*
        # 手动控制事务
        Db::startTrans();
        try{
            $list1 = Db::name('article')->find(1);
            Db::name('article')->delete(2);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
        }
        dump($list1);
        */


        # 获取器
        /*
        $art = Article::get(1);
        echo $art->state;
        echo '<hr>';
        echo $art->getData('state');
        echo '<hr>';
        dump($art->getData());
        */

        # 修改器
        /*
        $art = Article::get(1);
        echo $art->title.'<hr>';
        $art->title = 'aaaa';
        echo $art->title;
        */
        /*
        $art = Article::get(1);
        $data['title'] = 'php';
        $data['info']  = '这个语言不错呀';
        $art->save($data);
        echo $art->title;
        */


        $data = [
            'title' => 'aaaa'
        ];


        $ret = $this->validate($data, 'Article');

        if (true !== $ret) {
            return $ret;
        }

        echo '你通过了<hr>';

        $ret = Validate::gt(10, 2);
        dump($ret);


        return view();
    }


    public function formvalidate(Request $request) {
        $art = new Article();
        if ($request->isPost()) {
            $ret = $art->allowField(true)->validate(true)->save(input('post.'));
            if ($ret) {

            } else {
                return $art->getError();
            }
        }

        return view();
    }


    public function pagelist() {
        $list = Db::name('article')->paginate(4, false, [
            'var_page' => 'p'
        ]);
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function imgcode() {

        if ($this->request->isPost()) {

            $code = $this->request->post('code');

            if (!captcha_check($code)) {
                return $this->error('验证码不正确！');
            } else {
                return $this->success('成功', 'imgcode');
            }

        }
        return view();
    }


    public function up(Request $request) {

        if ($request->isPost()) {

            $file = $request->file('pic');

            $ret = $file->validate(['ext' => 'png,jpg,gif,jpeg'])->rule('1')->move(ROOT_PATH . 'public' . DS . 'uploads/',time());

            if (false === $ret) {
                return $file->getError();
            }

            return;
        }

        return view();
    }


}

?>